<?php
//我的任务
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['set']=$return['setting']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
	$id = intval($_RQ['id']);
	$detail = pdo_get('jy_inclock_task_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if (empty($detail)) {
			exi('任务不存在','error');
	}
	$record = pdo_get('jy_inclock_task_record',array('taskid'=>$detail['id'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$taskType = $this->model()->getTaskType($type);
	$desc = $taskType['verb'];
	if (!(empty($taskType['unit']))) {
		$desc .= $record['task_demand'] . $detail['demand'] . $taskType['unit'];
	}
	set_html('title','任务详情');
	$return['rid']=$rid;
	$return['img']=$img;
	$return['desc']=$desc;
	$detail['reward']=json_decode($detail['reward'],true);
	$detail['rewarddesc']='奖励';
	$reward=$detail['reward'];
	$credit_types=credit_types();
	foreach($reward as $key=>$value){
		if(strpos($key,'credit') !== false && $value>0){
				$type=substr($key, -1);
				$detail['rewarddesc'].='['.$value.$credit_types[$type].'] ';
		}
	}
	$return['detail']=$detail;
	if($record['task_progress']>=$record['task_demand']){
			$record['ispass']=1;
	}
	$isget=$this->get('task_reward',array('recordid'=>intval($record['id']),'mid'=>intval($record['mid']),'uniacid'=>$_SESSION['uniacid']));
	if($isget['id']){
			$record['isreward']=1;	
	}
	$return['record']=$record;
	$return['sharedata']=array(
		'title'=>$detail['title']?:'任务详情',
		'desc'=>$detail['title']?$detail['title'].'-'.$detail['rewarddesc']:'任务详情！',
		'link'=>custom_url('jy_inclock/detail//id='.$detail['id'],true),
		'imgUrl'=>ATTACHMENT_URL.$detail['image']
	);
	exi($return);
}
if($_RQ['op']=='send1'){
	$url=custom_url('jy_inclock/index/',true);
	$postdata=get_postdata();
//	print_r($postdata['img']);exit;
	$newsmsg['msgtype']='news';
	$articles[0]['title'] = "";
	//$articles[0]['description']=$postdata['img'];
	$articles[0]['URL'] =$url;
	$articles[0]['picurl'] =$postdata['img'];
	$newsmsg['news']['articles'] =$articles;
	cfc('account')->sendCustomNotice($newsmsg,$_SESSION['uid']);
	$return['ok']=1;
	exi($return);
}
if($_RQ['op']=='send'){
	$postdata=get_postdata();
	$openid=pdo_getcolumn('core_fans',array('mid'=>$_SESSION['uid']),'openid');
	$date['msgtype']='image';
	$date['touser']=$openid;
	$accesstoken=cfc('account')->getAccessToken();
	$date['image']['media_id']=cfc('account')->add_media($postdata['img'],'image');
	$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$accesstoken;
	$data=ijson_encode($data);
	cfc('account')->request($url,$data,true,false,true);
	$return['ok']=1;
	exi($return);
}

if($_RQ['op']=='getReward'){
	$postdata=get_postdata();
	$record_id=$postdata['detail_id'];
	$this->model('model')->checkfinish($record_id);
	exi('领取奖励成功！','error','/mine/main/');
}
